<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!-- 
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEidt(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item 
                        v-if="data.children.length === 0"
                        @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
        :title="isEdit ?'修改':'添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
        >
        <div>
            <AddorEdit
            v-if="showDialog"
            :id="curId" 
            :is-edit="isEdit" 
            @update-depart="hUpdateDepart"
            @close="hClose"
            />
        </div>
        <!-- <div slot="footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </div> -->
    </el-dialog>
  </div>
</template>

<script>
// 引入api
import {getDepartments , delDepartment} from '@/api/departments'
//引入封装好的树形结构代码
import{tranListToTreeData} from '@/utils/index.js'
// 引入弹窗组件
import AddorEdit from './deptDialog.vue'
export default {
  data() {
    return {
        isEdit:false,//表示当前是否处于编辑
        curId:'',//当前操作的部门id
      showDialog:false,
      // 依赖一份树形数据
      list: [],
    };
  },
  components:{
        AddorEdit
  },
  //生命周期 钩子函数
  created(){
      //封装一个方法
      this.loadDpeartments()
  },
//   watch: {
//     id(newVal, oldVal) {
//       console.log('id变化了', oldVal, '--->', newVal)
//       this.loadDetail()
//     }
//   },
  methods: {
     async loadDpeartments(){
         try{
             const res = await getDepartments() 
            //  console.log(res);
            //  取出第一个元素
            //shift 从数组头部取出第一个元素
            const a = res.data.depts.shift()
            // console.log(a);
            //  用封装好的工具函数，把平铺的数组转换成树形结构
            // const children = tranListToTreeData(res.data.depts)
            a.children = tranListToTreeData(res.data.depts)
            this.list = [a]
         }catch(err){
             console.log(err);
         }
      },
      hAdd(id){
        //   console.log('部门id值',id);
        // 更新当前部门操作的id
        this.curId = id
        // 显示弹层
          this.showDialog = true
        //  更新编辑状态
        this.isEdit = false
      },
      async doDel(id) {
          try {
            const res = await delDepartment(id)
            console.log(res)
            // 1. 提示
            this.$message({ type: 'success', message: '删除成功!' })
            // 2. 更新数据
            this.loadDpeartments()
          } catch (err) {
            console.log(err)
            this.$message({ type: 'error', message: '删除失败' })
          }
        },
         hDel(id) {
          this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 做删除
            this.doDel(id)
          }).catch(() => {

          })
      },
      hUpdateDepart(){
        //   关闭弹层
        this.showDialog = false
        // 更新数据
        this.loadDpeartments() 

      },
      hEidt(id){
             // 更新当前部门操作的id
        this.curId = id
        // 显示弹层
          this.showDialog = true
          this.isEdit = true
      },
      hClose(){
          this.showDialog = false
      }
  },
};
</script>

<style>
</style>